<?php



namespace app\superadmin\controller;


use app\common\controller\SuperadminBase;
use think\Controller;

use think\Request;

use think\Session;

use think\Db;

use app\superadmin\model\Car as Model;




class Car extends SuperadminBase

{

    public function index()

    {   

        // $info = Db::name('car')->where('state',1)->where('level',1)->field(['id','name','pid','px'])->select();

        //  for ($i=0; $i < count($info); $i++) { 
            
        //     $zimu = $this->getInitials($info[$i]['name']);

        //     if ($zimu) {
                
        //         Db::name('car')->where('id',$info[$i]['id'])->update(['zimu'=>$zimu]);
        //     }

        //  }

        return $this->fetch();

    }



    public function gList(Request $request) {

         //获取每页显示的条数

        $limit= $request->param('limit');

        //获取当前页数

        $page= $request->param('page');


        $name = $request->param('name');


        $adminid = session('adminid');
      
       

        $list = Model::with('upinfo')->where('name', 'like', '%' . $name . '%')->order('id_path,px,id')->paginate($limit,false,['page'=>$page])->toArray();    

        return json(['code'=>0,'msg'=>'','count'=>$list['total'],'data'=>$list['data']]);

    }




    public function add()
    {   

        if(\request()->isPost()){

            $data  = input('post.');

            $sameName = Model::where(['name'=>$data['name']])->find();

            if($sameName){
                return json(['code'=>0, 'message'=>'品牌、车型名称已存在']);
            }

            if ($data['pid'] == 0) {
                $data['level'] = 1;
            }else{
                $data['level'] = 2;
            }
            $info = Model::create($data);

            $id_path = 0;

            $id = $info->id;

            $weishu = strlen(floor($id));

            $newid = 0;

             if ($weishu == 1) {
                 
                 $newid = '000'.$id;

             }else if ($weishu == 2) {

                 $newid = '00'.$id;

             }else if ($weishu == 3) {

                 $newid = '0'.$id;

             }else if ($weishu == 4) {

                 $newid = $id;
             }





            if ($data['pid'] == 0) {

                $id_path = $newid;

            }else{

                $upinfo = Model::where('id',$data['pid'])->find();

                $id_path = $upinfo['id_path'].$newid;
            }

            Model::where('id',$id)->update(['id_path'=>$id_path]);

            return json(['code'=>1, 'message'=>'录入成功！']);

        }else{

            $storeList = Model::where('pid',0)->select();
            $this->assign('storeList', $storeList);
            return $this->fetch();

        }
        
    }


    // 编辑
    public function edit()
    {   

        if(\request()->isPost()){

            $data  = input('post.');

            $sameName = Model::where(['name'=>$data['name']])->where('id','neq',$data['id'])->find();

            if($sameName){
                return json(['code'=>0, 'message'=>'品牌、车型名称已存在']);
            }


            $id_path = 0;

            $id = $data['id'];

            $weishu = strlen(floor($id));

            $newid = 0;

             if ($weishu == 1) {
                 
                 $newid = '000'.$id;

             }else if ($weishu == 2) {

                 $newid = '00'.$id;

             }else if ($weishu == 3) {

                 $newid = '0'.$id;

             }else if ($weishu == 4) {

                 $newid = $id;
             }

             if ($data['pid'] == 0) {
                 
                 $data['id_path'] = $newid;
                 $data['level'] = 1;

             }else{



                 $upinfo = Model::where('id',$data['pid'])->find();

                 $id_path = $upinfo['id_path'].$newid;

                 $data['id_path'] = $id_path;

                 $data['level'] = 2;

             }

             //halt($data);

             Model::where('id',$id)->update($data);

             return json(['code'=>1, 'message'=>'修改成功']);


        }else{

            $param = $this->request->param();

            $info = Model::get(intval($param['id']));

            $storeList = Model::where('pid',0)->select();


            $this->assign('storeList', $storeList);

            $this->assign('info', $info);

            return $this->fetch();

        }
        
    }






    //删除

    public function del() {

        $id = $this->request->param('id');

        $info = Model::where('pid',$id)->find();

        if ($info) {
            return 2;
        }

        Model::destroy($id);

        return 1;

    }



    public function editSxj()

    {
        $id    = $this->request->param('id');
        $is_xj = $this->request->param('is_xj');

        $info = Model::where('id', $id)->find();

        $arr = Model::where('id_path','like',$info['id_path'] . '%')->select();

        for ($i=0; $i < count($arr); $i++) { 
                
                Model::where('id', $arr[$i]['id'])->update(['state' => $is_xj]);
        }

        // return json(['code'=>0, 'message'=>'品牌、车型名称已存在']);
        return 1;

       
    }



    public function editBase()

    {   
        $arr = input('post.');

        //halt($arr);
        $id    = $arr['id'];
        $base = $arr['base'];
        $ziduan = $arr['ziduan'];

        $info = Model::where('id', $id)->find();

        $arr = Model::where('id_path','like',$info['id_path'] . '%')->select();

        for ($i=0; $i < count($arr); $i++) { 
                
                Model::where('id', $arr[$i]['id'])->update([$ziduan => $base]);
        }

        //return json(['code'=>0, 'message'=>'品牌、车型名称已存在']);
        return 1;

       
    }



    public function editpx()

    {
        $id    = $this->request->param('id');
        $px = $this->request->param('px');

        $info = Model::where('id', $id)->find();

        Model::where('id', $id)->update(['px' => $px]);
       
        return 1;

       
    }





}

